/*
 * @Author: 晟松
 * @Date: 2021-12-06 12:51:34
 * @LastEditors: 晟松
 * @LastEditTime: 2021-12-08 17:24:47
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\service\TestService.js
 * @Description: test
 */

import sequelize from "@main/db";
import { reset } from "module-alias";

const userModel = sequelize.model("user");
const testModel = sequelize.model("test");

class TestService {
  static async test() {
    const sqlStr = "select * from t_user limit 1,3";
    const sqlStr1 = "select * from t_user where phone = ?";
    // replacements是用来代替sql语句中的？的
    let data = await sequelize.query(sqlStr, {
      replacements: ["13206078281"],
      //   model: userModel,
    });
    // 使用sequelize.query进行查询时，若不加上model，则会返回一个包含两个元素的数组 [dataValues,metadataValues]，需要处理一下

    // 添加了model的data
    /* data:  [
        user {
          dataValues: {
            id: 1,
            phone: '13206078281',
            password: 'e10adc3949ba59abbe56e057f20f883e',
            nick_name: '晟松',
            sex: 1,
            created_at: '2021-11-30 21:10:54',
            updated_at: '2021-11-30 21:10:57'
          },
          _previousDataValues: {
            id: 1,
            phone: '13206078281',
            password: 'e10adc3949ba59abbe56e057f20f883e',
            nick_name: '晟松',
            sex: 1,
            created_at: '2021-11-30 21:10:54',
            updated_at: '2021-11-30 21:10:57'
          },
          _changed: Set(0) {},
          _options: {
            isNewRecord: false,
            _schema: null,
            _schemaDelimiter: '',
            raw: true,
            attributes: undefined
          },
          isNewRecord: false
        } */

    // console.log("data: ", data);
    return data[0];
  }

  /**
   * @description: 手动创建对象和对应外键的表的存储
   * @param {*}
   * @return {*}
   */
  static async refer() {
    // 会自动去除与model无关的字段
    let user = {
      phone: "13206078911",
      password: "44444212211221cdcd",
      sex: 1,
      nickName: "晟松123",
      other:'cs21'
    };
    let test = { first: "first", last: "last",other1:'778' }
    /* let resUserData = await userModel.create(user);
    console.log("resUserData: ", resUserData.toJSON());

    // 手动添加外键值
    let userRefer = {
      first: "2121",
      last: "村上春树",
      userId: resUserData.toJSON().id,
    };

    let resUserReferData = await testModel.create(userRefer);

    console.log("resUserReferData: ", resUserReferData.toJSON()); */

    // 或者这一种方式的创建
    /* let otherUser = Object.assign(user, {
      test: { first: "first", last: "last" },
    });
    let resUserData1 = await userModel.create(otherUser, {
      include: [
        {
          association: userModel.test,
        },
      ],
    }); */

    let otherTest = Object.assign(test,{user})
    let resUserData1 = await testModel.create(otherTest, {
      include: [
        {
          association: testModel.user,
          // 注意这里是association，而非include
          // include:[testModel.user]
        },
      ],
    });
    return resUserData1.toJSON();
  }

  /**
   * @description: 子查询，即有外键时的查询
   * @param {*} id
   * @return {*}
   */
  static async childSelect(id = 43) {
    let resUser = await userModel.findOne({
      where: {
        id: 48,
      },
      include: testModel,
    });

    let resRefer = resUser.toJSON();
    console.log("resRefer: ", resRefer);

    let resTest = await testModel.findAll({
      include: userModel,
    });
    console.log("resTest: ", resTest);

    return [resRefer, resTest];
  }
}

export default TestService;
